package com.lm.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lm.dao.BaoMingDAO;
import com.lm.model.Baoming;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.util.DBUtil;
import com.util.DataGeneration;

public class BaoMingDAOImpl implements BaoMingDAO, DataGeneration {
	private ComboPooledDataSource dataSource;

	private List<Baoming> resList = new ArrayList<Baoming>();

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void addBaoMing(Baoming bm) throws Exception {
		DBUtil.addOperation(dataSource, getAddBaoMingSQL(bm));
	}

	public void deleteBaoMingByEventID(String eventID) throws Exception {
		DBUtil.editOrDeleteOperation(dataSource,
				getDeleteBaoMingByEventIDSQL(eventID));
	}

	public boolean hasBaoMing(String name, String eventID) throws Exception {
		return DBUtil.getBooleanOperation(dataSource, getHasBaoMingSQL(name,
				eventID));
	}

	public List<Baoming> getBaoMingByEventID(int eventID) throws Exception {
		return DBUtil.getCollectionOperation(dataSource,
				getSelectBaoMingByEventIDSQL(eventID), new BaoMingDAOImpl());
	}

	private String getAddBaoMingSQL(Baoming bm) {
		StringBuilder sb = new StringBuilder();

		sb.append(" insert into lm_baoming(name,sex,age,mobile,");
		sb.append("tel,email,homeAddress,brandIDs,eventID)");
		sb.append(" values('").append(bm.getName()).append("',");
		sb.append(bm.getSex()).append(",");
		sb.append(bm.getAge()).append(",");
		sb.append("'").append(bm.getMobile()).append("',");
		sb.append("'").append(bm.getTel()).append("',");
		sb.append("'").append(bm.getEmail()).append("',");
		sb.append("'").append(bm.getHomeAddress()).append("',");
		sb.append("'").append(bm.getBrandIds()).append("',");
		sb.append(bm.getEventId()).append(")");
		return sb.toString();
	}

	private String getHasBaoMingSQL(String name, String eventID) {
		StringBuilder sb = new StringBuilder();

		sb.append(" select * from  lm_baoming");
		sb.append(" where name like '%").append(name).append("%'");
		sb.append(" and eventID=").append(Integer.parseInt(eventID));
		return sb.toString();
	}

	private String getSelectBaoMingByEventIDSQL(int eventID) {
		StringBuilder sb = new StringBuilder();

		sb.append(" select * from lm_baoming");
		sb.append(" where eventID=").append(eventID);
		return sb.toString();
	}

	private String getDeleteBaoMingByEventIDSQL(String eventID) {
		StringBuilder sb = new StringBuilder();

		sb.append(" delete from lm_baoming");
		sb.append(" where eventID=").append(Integer.parseInt(eventID));
		return sb.toString();
	}

	public List<Baoming> getResList(ResultSet rs) throws SQLException {
		while (rs.next()) {
			Baoming bm = new Baoming();

			bm.setId(rs.getInt("id"));
			bm.setName(rs.getString("name"));
			bm.setAge(rs.getInt("age"));
			bm.setSex(rs.getInt("sex"));
			bm.setMobile(rs.getString("mobile"));
			bm.setTel(rs.getString("tel"));
			bm.setEmail(rs.getString("email"));
			bm.setHomeAddress(rs.getString("homeAddress"));
			bm.setBrandIds(rs.getString("brandIDs"));
			resList.add(bm);
		}

		return resList;
	}

	public Object getResModel(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
}
